package com.goods.business.service.imp;

import com.goods.business.converter.HealthConverter;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.model.business.Product;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HealthServiceImpl implements HealthService {
    @Resource
    private HealthMapper healthMapper;

    //当前日期打卡信息查询
    @Override
    public boolean isReport(Long id) {
        Example example = new Example(Health.class);
        example.createCriteria().andEqualTo("userId",id);
        List<Health> healthList = healthMapper.selectByExample(example);
        for (Health health : healthList) {
            Date createTime = health.getCreateTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String createTimeFormat = simpleDateFormat.format(createTime);
            String currentTime = simpleDateFormat.format(new Date());
            if (createTimeFormat.equals(currentTime)){
                return true;
            }
        }
        return false;
    }
    //立即打卡
    @Override
    public void report(HealthVO healthVO,Long id) {
        Health health = new Health();
        BeanUtils.copyProperties(healthVO,health);
        health.setCreateTime(new Date());
        health.setUserId(id);
        healthMapper.insert(health);
    }
    //打卡历史记录
    @Override
    public PageVO<HealthVO> history(Map<String,Object> queryMap,Long id) {
        Example example = new Example(Health.class);
        example.createCriteria().andEqualTo("userId",id);
        List<Health> healthList = healthMapper.selectByExample(example);
        List<HealthVO> healthVOList = HealthConverter.converterToHealthVOList(healthList);
        String pageSize = (String)queryMap.get("pageSize");
        String pageNum = (String)queryMap.get("pageNum");
        List<Health> page = ListPageUtils.page(healthList, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
        return new PageVO<>(page.size(),healthVOList);
    }
}
